﻿function hasClass(ele, cls) {
    return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(ele, cls) {
    if (!this.hasClass(ele, cls)) ele.className += " " + cls;
}

function removeClass(ele, cls) {
    if (hasClass(ele, cls)) {
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
        ele.className = ele.className.replace(reg, ' ');
    }
}

function LoadMenu(id) {
    if (id == 0) {
        addClass(document.getElementById("muser1"), "MActive")
        removeClass(document.getElementById("muser2"), "MActive")
        removeClass(document.getElementById("muser3"), "MActive")
        removeClass(document.getElementById("muser4"), "MActive")
        removeClass(document.getElementById("muser5"), "MActive")
    }
    if (id == 1) {
        removeClass(document.getElementById("muser1"), "MActive")
        addClass(document.getElementById("muser2"), "MActive")
        removeClass(document.getElementById("muser3"), "MActive")
        removeClass(document.getElementById("muser4"), "MActive")
        removeClass(document.getElementById("muser5"), "MActive")
    }
    if (id == 2) {
        removeClass(document.getElementById("muser1"), "MActive")
        removeClass(document.getElementById("muser2"), "MActive")
        addClass(document.getElementById("muser3"), "MActive")
        removeClass(document.getElementById("muser4"), "MActive")
        removeClass(document.getElementById("muser5"), "MActive")
    }
    if (id == 3) {
        removeClass(document.getElementById("muser1"), "MActive")
        removeClass(document.getElementById("muser2"), "MActive")
        removeClass(document.getElementById("muser3"), "MActive")
        addClass(document.getElementById("muser4"), "MActive")
        removeClass(document.getElementById("muser5"), "MActive")
    }
    if (id == 4) {
        removeClass(document.getElementById("muser1"), "MActive")
        removeClass(document.getElementById("muser2"), "MActive")
        removeClass(document.getElementById("muser3"), "MActive")
        removeClass(document.getElementById("muser4"), "MActive")
        addClass(document.getElementById("muser5"), "MActive")
    }

}
function RemoveMenuActive() {
    removeClass(document.getElementById("muser2"), "MActive")
    removeClass(document.getElementById("muser3"), "MActive")
    removeClass(document.getElementById("muser4"), "MActive")
    removeClass(document.getElementById("muser5"), "MActive")
}
function LoadPage(page) {

    document.getElementById("page").value = page;
    document.getElementById("page_sendedit").value = page;
    document.forms["FormLoadPage"].btnLoadPage.click();
    document.body.style.cursor = "wait";
}
function LoadLimitPage(limit) {
    document.getElementById("pagesize").value = limit;
    document.getElementById("pagesize_sendedit").value = limit;
    if (limit == 50) {
        addClass(document.getElementById("50"), "Active")
        removeClass(document.getElementById("100"), "Active")
        removeClass(document.getElementById("150"), "Active")
        removeClass(document.getElementById("200"), "Active")
    }
    if (limit == 100) {
        removeClass(document.getElementById("50"), "Active")
        addClass(document.getElementById("100"), "Active")
        removeClass(document.getElementById("150"), "Active")
        removeClass(document.getElementById("200"), "Active")
    }
    if (limit == 150) {
        removeClass(document.getElementById("50"), "Active")
        removeClass(document.getElementById("100"), "Active")
        addClass(document.getElementById("150"), "Active")
        removeClass(document.getElementById("200"), "Active")
    }
    if (limit == 200) {
        removeClass(document.getElementById("50"), "Active")
        removeClass(document.getElementById("100"), "Active")
        removeClass(document.getElementById("150"), "Active")
        addClass(document.getElementById("200"), "Active")
    }
    document.forms["FormLoadLimitPage"].btnLoadLimitPage.click();
    document.body.style.cursor = "wait";
}

function LoadContent(menu) {
    document.getElementById("menu").value = menu;
    document.forms["FormLoadContentMenu"].btnLoadContentMenu.click();
    document.body.style.cursor = "wait";
}
function SubmitSearch() {
    document.forms["FormLoadPageSearch"].btnLoadPageSearch.click();
    document.body.style.cursor = "wait";
}
function Delete(idpage, id) {
    if (idpage == 3) {
        document.getElementById("idNKBH").value = id;
    }
    if (idpage == 5) {
        document.getElementById("idChuHang").value = id;
    }
    if (idpage == 6) {
        document.getElementById("idKhachHang").value = id;
    }
    document.forms["FormDelete"].btnDelete.click();
    document.body.style.cursor = "wait";
}
function Edit(idpage, id) {
   
    if (idpage == 3) {
        document.getElementById("idNKBH_Edit").value = id;
    }
    if (idpage == 5) {
        document.getElementById("idChuHang_Edit").value = id;
    }
    if (idpage == 6) {
        document.getElementById("idKhachHang_Edit").value = id;
    }
    document.forms["FormEdit"].btnEdit.click();
    $('.FormAdd').show();
    document.body.style.cursor = "wait";
}
function OK(idpage, id) {
    if (idpage == 3) {
        document.getElementById("idNKBH_OK").value = id;
    }
    if (idpage == 5) {
        document.getElementById("idChuHang_OK").value = id;
    }
    if (idpage == 6) {
        document.getElementById("idKhachHang_OK").value = id;
    }
    document.forms["FormOK"].btnOK.click();
    document.body.style.cursor = "wait";
}
function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;
    if (sText != '') {
        for (i = 0; i < sText.length && IsNumber == true; i++) {
            Char = sText.charAt(i);
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
    }
    return IsNumber;
}
function IsDate(txtDate) {
    var objDate,  // date object initialized from the txtDate string
        mSeconds, // txtDate in milliseconds
        day,      // day
        month,    // month
        year;     // year
    // date length should be 10 characters (no more no less)
    if (txtDate.length !== 10) {
        return false;
    }
    // third and sixth character should be '/'
    if (txtDate.substring(2, 3) !== '/' || txtDate.substring(5, 6) !== '/') {
        return false;
    }
    // extract month, day and year from the txtDate (expected format is mm/dd/yyyy)
    // subtraction will cast variables to integer implicitly (needed
    // for !== comparing)
    day = txtDate.substring(0, 2) - 1; // because months in JS start from 0
    month = txtDate.substring(3, 5) - 0;
    year = txtDate.substring(6, 10) - 0;
    // test year range
    if (year < 1000 || year > 3000) {
        return false;
    }
    // convert txtDate to milliseconds
    mSeconds = (new Date(year, month, day)).getTime();
    // initialize Date() object from calculated milliseconds
    objDate = new Date();
    objDate.setTime(mSeconds);
    // compare input date and parts from Date() object
    // if difference exists then date isn't valid
    if (objDate.getFullYear() !== year ||
        objDate.getMonth() !== month ||
        objDate.getDate() !== day) {
        return false;
    }
    // otherwise return true
    return true;
}
function IsValidEmail(str) {

    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);



}
function clearForm(oForm) {

    var elements = oForm.elements;

    oForm.reset();

    for (i = 0; i < elements.length; i++) {

        field_type = elements[i].type.toLowerCase();

        switch (field_type) {

            case "text":
            case "password":
            case "textarea":
            case "hidden":

                elements[i].value = "";
                break;

            case "radio":
            case "checkbox":
                if (elements[i].checked) {
                    elements[i].checked = false;
                }
                break;

            case "select-one":
            case "select-multi":
                elements[i].selectedIndex = -1;
                break;

            default:
                break;
        }
    }
}